const { Wechaty,MediaMessage,Room,Contact,FriendRequest } = require('wechaty')

const bot = Wechaty.instance();

const contactNameList=["屎忽安","猥琐昱","小后"]

// 获取二维码
bot.on('scan', (url, code) => log(`登陆二维码: ${code} \n ${url}`))

// 监听用户登录成功
bot.on('login',async user => {

    log(`用户 ${user} 已经登陆了`);

    await createDingRoom();

})

// 创建群
async function createDingRoom() {
    
    log('正在创群中........')
  
    try {

        const contactList = await getContact("two")
  
        if (!contactList) {
            log('getContact  没有找到好友.........')
            return
        }

        log(`好友 ${contactList} 准备加群......`)  
  
        const room = await Room.create(contactList, '机器人创建的群')
        
        log(`群已经建好啦 : %s ${room}.......` )
    
        await room.topic('机器人把名字改了')
        await room.say('大家好，我是机器人')
        await room.say('笨戳陈昱')
        await room.say('笨戳陈家安')
    
        return room
  
    } catch (e) {

        log(`建群出现异常 : ${e.stack} .....` )
        throw e
        
    }
}
  
// 获取联系人
async function getContact(type) {

    log('开始获取联系人......')

    let  contactList=[]

    for (let i = 0; i < contactNameList.length; i++){

        const contact = await Contact.find({ alias: contactNameList[i] })

        contactList.push(contact)
        
    }
    
    if (type == "one") return contactList[0] 
    else if (type == "two") return contactList.splice(0,2)
    else if (type == "all") return contactList
    
}

function log(val) {
    console.log(`\n\n----${val}\n`)
}



bot.start()